// pages/mine/mine.js
var app = getApp()
Page({
  data: {

    dataList: [
      {
        goods_id: 1,
        goods_title: '商品标题1',
        goods_img: 'http://101.132.97.213:9090/image/1233212212212/1575952653630142.jpg',
        goods_xiaoliang: '20',
        goods_price: '600'
      }, {
        goods_id: 1,
        goods_title: '商品标题2',
        goods_img: 'http://101.132.97.213:9090/image/1233212212212/1575952652583827.jpg',
        goods_xiaoliang: '12',
        goods_price: '70'
      }, {
        goods_id: 1,
        goods_title: '商品标题3',
        goods_img: 'http://101.132.97.213:9090/image/1233212212212/1575952653630142.jpg',
        goods_xiaoliang: '0',
        goods_price: '800'
      }, {
        goods_id: 1,
        goods_title: '商品标题4',
        goods_img: 'http://101.132.97.213:9090/image/1233212212212/1575952652583827.jpg',
        goods_xiaoliang: '10',
        goods_price: '90'
      }, {
        goods_id: 1,
        goods_title: '商品标题5',
        goods_img: 'http://101.132.97.213:9090/image/1233212212212/1575952652583827.jpg',
        goods_xiaoliang: '33',
        goods_price: '110'
      }
    ],

    userInfo: {},
    user:{},
    // orderItems
    orderItems: [
      {
        typeId: 0,
        name: '全部',
        url: '../myorder/index?currentTab=0',
        imageurl: '../../../assets/icons/quan.png',
      },
      {
        typeId: 1,
        name: '待付款',
        url: '../myorder/index?currentTab=1',
        imageurl: '../../../assets/icons/fu.png',
      },
      {
        typeId: 2,
        name: '待发货',
        url: '../myorder/index?currentTab=2',
        imageurl: '../../../assets/icons/shou1.png',
      },
      {
        typeId: 3,
        name: '待收货',
        url: '../myorder/index?currentTab=3',
        imageurl: '../../../assets/icons/shou.png',
      },
      {
        typeId: 4,
        name: '完成',
        url: '../myorder/index?currentTab=4',
        imageurl: '../../../assets/icons/wancheng.png',
      },
     
    ],
  },
  //查询数据 
  listdata: function () {
    var that = this;
    wx.request({
      url: app.globalData.the_host + `/product/recommended?count=100&tenant=t1`,
      data: {},
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {

        that.saixuan(res.data)
        that.setData({
          datalist: res.data,
        });
        wx.hideToast();//隐藏提示框
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  //筛选
  saixuan: function (list) {
    var arr = []
    for (let i = 0; i < list.length; i++) {
      for (let a = 0; a < list[i].resources.length; a++) {
        if (list[i].resources[a].type == 0) {
          arr.push(list[i].resources[a].url);
          break;
        }
      }
    }
    this.setData({
      imglist: arr,
    });
    console.log(arr)
  },
  //推出登录
  bindtapTuiChu:function(){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要退出登录？',
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          //点击确定
          wx.setStorageSync("user", null)
          wx.redirectTo({
            url: '/pages/user/login/index'
          })
        }
      },
    })
   
  },
  //事件处理函数
  toOrder: function (e) {
    console.log(e.currentTarget.dataset.typeid)
    wx.navigateTo({
      url: '../myorder/index?currentTab=' + e.currentTarget.dataset.typeid
    })
  },
  //事件处理函数
  bindtapDiZhi: function (e) {    
    wx.navigateTo({
      url: '../addressList/index'
    })
  },
  onLoad: function () {
    this.listdata()
    let user = wx.getStorageSync("user")
    console.log(user)
    this.setData({
      user: wx.getStorageSync("user")
    })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true,
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },

  // 选项卡
  filterTab: function (e) {
    var data = [true, true, true], index = e.currentTarget.dataset.index;
    data[index] = !this.data.tab[index];
    this.setData({
      tab: data
    })
  },

  //筛选项点击操作
  filter: function (e) {
    var self = this, id = e.currentTarget.dataset.id, txt = e.currentTarget.dataset.txt, tabTxt = this.data.tabTxt;
    switch (e.currentTarget.dataset.index) {
      case '0':
        tabTxt[0] = txt;
        self.setData({
          tab: [true, true, true],
          tabTxt: tabTxt,
          pinpai_id: id,
          pinpai_txt: txt
        });
        break;
      case '1':
        tabTxt[1] = txt;
        self.setData({
          tab: [true, true, true],
          tabTxt: tabTxt,
          jiage_id: id,
          jiage_txt: txt
        });
        break;
      case '2':
        tabTxt[2] = txt;
        self.setData({
          tab: [true, true, true],
          tabTxt: tabTxt,
          xiaoliang_id: id,
          xiaoliang_txt: txt
        });
        break;
    }
    //数据筛选
    self.getDataList();
  },

})
